package org.oslims.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.google.gwt.validation.client.NotNull;

@Entity
public class MixtureComponent extends AbstractBaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -61292256401807127L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	private Long id;

	@NotNull
	private Double ammount;

	@ManyToOne
	@JoinColumn(name = "SAMPLE_FK")
	@NotNull
	private AbstractSample sample;

	@ManyToOne
	@JoinColumn(name = "ROOT_SAMPLE_FK")
	@NotNull
	private Mixture rootSample;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Double getAmmount() {
		return ammount;
	}

	public void setAmmount(Double ammount) {
		this.ammount = ammount;
	}

	public AbstractSample getSample() {
		return sample;
	}

	public void setSample(AbstractSample sample) {
		this.sample = sample;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof BaseEntity))
			return false;
		MixtureComponent other = (MixtureComponent) obj;
		if (this.getId() == null || other.getId() == null) {
			if (this.getSample() == null || other.getSample() == null) {
				return false;
			} else {
				this.getSample().equals(other.getSample());
			}
		}
		return this.getId() != null && this.getId().equals(other.getId());
	}

	@Override
	public int hashCode() {

		if (this.getId() == null) {
			if (this.getSample() == null) {
				return super.hashCode();
			} else {
				return this.getSample().hashCode();
			}
		} else {
			return this.getId().hashCode();
		}
	}

	@Override
	public int compareTo(AbstractBaseEntity arg0) {
		MixtureComponent other = (MixtureComponent) arg0;

		if (this.getId() == null || arg0.getId() == null) {
			return this.getSample().compareTo(other.getSample());
		}
		return this.getId().compareTo(arg0.getId());
	}

	public void setRootSample(Mixture rootSample) {
		this.rootSample = rootSample;
	}

	public Mixture getRootSample() {
		return rootSample;
	}
}
